package com.example.testrecorder;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.testrecorder.domain.QueAndAns;
import com.example.testrecorder.domain.User;
import com.example.testrecorder.utils.Des3Util;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class RegisterActivity extends BaseActivity implements View.OnClickListener{
    private EditText accountEt,passwordEt,passwordEt2;
    private LinearLayout loginLl;
    private Button registerBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }

    private void initView() {
        loginLl=findViewById(R.id.loginLl);
        loginLl.setOnClickListener(this);
        accountEt=findViewById(R.id.accountEt);
        passwordEt=findViewById(R.id.passwordEt);
        passwordEt2=findViewById(R.id.passwordEt2);
        registerBtn=findViewById(R.id.registerBtn);
        registerBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.registerBtn:
                String account = accountEt.getText().toString().trim();
                String password1 = passwordEt.getText().toString().trim();
                String password2 = passwordEt2.getText().toString().trim();
                if(TextUtils.isEmpty(account)){
                    Toast.makeText(RegisterActivity.this,"注册失败，账号不能为空！",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(password1)){
                    Toast.makeText(RegisterActivity.this,"注册失败，密码输入框1不能为空！",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(password2)){
                    Toast.makeText(RegisterActivity.this,"注册失败，密码输入框2不能为空！",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(!password1.equals(password2)){
                    Toast.makeText(RegisterActivity.this,"注册失败，两次输入的密码不一致！",Toast.LENGTH_SHORT).show();
                    return;
                }
                List<User> users = LitePal.where("userName=?", account).find(User.class);
                if (users.size()>0){
                    Toast.makeText(RegisterActivity.this,"注册失败，用户名"+account+"已被占用！",Toast.LENGTH_SHORT).show();
                }else {
                    User user=new User();
                    user.setUserName(account);
                    user.setPassword(Des3Util.encrypt("lyy",password1));//将密码进行加密
                    user.setRegisterDate(new Date());//注册时间
                    boolean save = user.save();
                    if(save){
                        Toast.makeText(RegisterActivity.this,"恭喜您，注册成功！",Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(RegisterActivity.this,"注册失败！",Toast.LENGTH_SHORT).show();
                    }
                }
                break;
            case R.id.loginLl:
                startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
                break;
            default:
        }
    }
}